package com.zahb.qadx.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.EditTextHelper;
import com.zahb.qadx.util.InputMethodUtil;
import com.zahb.qadx.util.RegularUtil;
import com.zahb.qadx.util.SimpleTextWatcher;
import com.zahb.qadx.util.Tips;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ResetPwd2Activity extends BaseActivity {

    @BindView(R.id.et_pwd)
    AppCompatEditText etPwd;
    @BindView(R.id.et_sure_pwd)
    AppCompatEditText etSurePwd;
    @BindView(R.id.btn_sure)
    AppCompatButton btnSure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_reset_pwd2;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);
        mTopBarContainer.setVisibility(View.VISIBLE);
        EditTextHelper.getEditTextDeleteIconWrapper(etPwd).setTextWatcher(mSimpleTextWatcher);
        EditTextHelper.getEditTextDeleteIconWrapper(etSurePwd).setTextWatcher(mSimpleTextWatcher);
    }

    @Override
    protected int getTitleStringRes() {
        return R.string.reset_pwd;
    }

    @OnClick(R.id.btn_sure)
    public void onViewClicked() {
        InputMethodUtil.hideInputMethod(getActivity());
        resetPwd();
    }

    private SimpleTextWatcher mSimpleTextWatcher = new SimpleTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            btnSure.setEnabled(!CompatHelper.isEmpty(etPwd) && !CompatHelper.isEmpty(etSurePwd));
        }
    };

    private void resetPwd() {
        if (!TextUtils.equals(CompatHelper.getString(etPwd), CompatHelper.getString(etSurePwd))) {
            showBindToast("两次输入的密码不一致");
            return;
        }

        String pwd = CompatHelper.getString(etPwd);
        if (RegularUtil.isValidPassword(pwd)) {
            String phone = getIntent().getStringExtra("phone");
            showProgressDialog("");
            addDisposable(RetrofitService.getNetService()
                    .resetPwd(phone, pwd)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        hideProgressDialog();
                        if (response.getStatusCode() == 200) {
                            showBindToast("重置密码成功");
                            LiveEventBus.get(Constant.KEY_RESET_PWD, String.class).post("");
                            finish();
                        } else {
                            showBindToast(response.getErrorInfo());
                        }
                    }, throwable -> {
                        throwable.printStackTrace();
                        hideProgressDialog();
                        Tips.RequestError(getActivity());
                    }));
        } else {
            showBindToast("密码格式错误");
        }
    }

    public static void actionStart(@NonNull Context context, @NonNull String phone) {
        Intent intent = new Intent(context, ResetPwd2Activity.class);
        intent.putExtra("phone", phone);
        context.startActivity(intent);
    }
}